package cn.com.dhcc.app.pub.module.usermgr.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import cn.com.dhcc.app.core.base.BaseDao;
import cn.com.dhcc.app.core.base.CmCommon;
import cn.com.dhcc.app.core.base.log.LogType;
import cn.com.dhcc.app.core.component.db.orm.WhereCondition;
import cn.com.dhcc.app.core.component.log.Log;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.tag.CommonStaticCode.ENTITY_STATUS;
import cn.com.dhcc.app.pub.core.Table.SYS_BASE_TABLE;
import cn.com.dhcc.app.pub.module.authmgr.vo.RoleInfoInner;
import cn.com.dhcc.app.pub.module.authmgr.vo.RoleInfoRowWapper;
import cn.com.dhcc.app.pub.module.usermgr.vo.RelUserRole;
import cn.com.dhcc.app.pub.module.usermgr.vo.wapper.RelUserRoleInfoRowWapper;

@Repository
public class RelUserRoleInfoDao extends BaseDao {

	private static Logger logger = Log.getLogger(LogType.BUSILOG);
	
	public List<RelUserRole> getRelSysUserRoleListByUserId(String userId) throws DaoException {
		String sql = "select distinct t1.role_id, t1.user_id "
				+" from " + WSD_TABLE(SYS_BASE_TABLE.SYS_BASE_REL_USER_ROLE) + " t1"
				+ " where t1.user_id = ?";
		try {
			return CmCommon.getDao().list("根据用户名获取角色ID列表", sql, new RelUserRoleInfoRowWapper(), new Object[] {userId});
		} catch (DaoException e) {
			logger.error(e.getMessage(), e);
			throw new DaoException("根据角色ID获取角色信息失败", e);
		}
	}

	public List<RoleInfoInner> getOtherRoleListByUserName(String userName, String userType) throws DaoException, NiceException {
		String sql = "select t1.* "
				+" from " + WSD_TABLE(SYS_BASE_TABLE.SYS_BASE_ROLE) + " t1"
				+ " where t1.id not in ( "
				+ " select distinct t.role_id id "
				+" from " + WSD_TABLE(SYS_BASE_TABLE.SYS_BASE_REL_USER_ROLE) + " t"
				+ " where t.user_id = ? )"
				+ " and t1.role_status = '" + ENTITY_STATUS.ENABLE.getCode() + "'";
		
		WhereCondition whereCondition = new WhereCondition();
		
//		if(userType.equals("0")||userType.equals("1")||userType.equals("2")||userType.equals("3")){
//			whereCondition.and().in("t1.role_type", new Object[] {1,2,4,8});
//		}else if(userType.equals("4")||userType.equals("5")||userType.equals("6")||userType.equals("7")){
//			whereCondition.and().in("t1.role_type", new Object[] {1,2,4});
//		}else if(userType.equals("8")||userType.equals("9")||userType.equals("10")||userType.equals("11")){
//			whereCondition.and().in("t1.role_type", new Object[] {1,2,8});
//		}else if(Integer.parseInt(userType)>=12){
//			throw new NiceException("用户角色中同时包含安全保密管理员、安全审计员");
//		}
		
		try {
			return CmCommon.getDao().list("根据用户名获取角色ID列表", sql, new RoleInfoRowWapper(), whereCondition, new Object[] {userName});
		} catch (DaoException e) {
			logger.error(e.getMessage(), e);
			throw new DaoException("根据角色ID获取角色信息失败", e);
		}
	}

	public int deleteRelSysUserRole(RelUserRole vo) throws DaoException {
		String sql = "delete from " + WSD_TABLE(SYS_BASE_TABLE.SYS_BASE_REL_USER_ROLE)
				+ " where user_id = ? and role_id = ? ";
		try {
			return CmCommon.getDao().delete("删除用户角色关系", sql, new Object[] {vo.getUserId(), vo.getRoleId()});
		} catch (DaoException e) {
			logger.error(e.getMessage(), e);
			throw new DaoException("删除用户角色关系失败", e);
		}
	}
}
